#include <stdlib.h>

#include "arraylist.h"
#include "arraystack.h"

#define INITIAL_STACK_CAP		10

/*
 * Declaratii de structuri
 */
 
struct arraystack_adt {
	ArrayList list;
	int count;
};

/*
 * Functii publice
 */
 
 
ArrayStack AS_New() {
	ArrayStack result;
	
	result = (ArrayStack) malloc(sizeof(struct arraystack_adt));
	
	result->list = AL_New(INITIAL_STACK_CAP);
	result->count = 0;
	
	return result;
}

void AS_Free(ArrayStack stack) {
	if (stack == NULL)
		return;
		
	AL_Free(stack->list);
	
	free(stack);
}

int AS_Size(ArrayStack stack) {
	if (stack == NULL)
		return -1;
		
	return stack->count;
}

int AS_Empty(ArrayStack stack) {
	if (stack == NULL)
		return -1;
		
	return (stack->count == 0) ? 1 : 0;
}

void AS_Push(ArrayStack stack, void *el) {
	if (stack == NULL)
		return;
		
	AL_Add(stack->list, AL_Begin(stack->list), el);
	
	stack->count++;
}

void AS_Pop(ArrayStack stack) {
/*
TODO: Nu e inconsistent dpdv al alocarii memoriei? Ma refer la: cum se comporta
AS_Free pe o stiva in care au fost adaugate elemente dar care la momentul
eliberarii este goala?
*/
	if (stack == NULL || stack->count == 0)
		return;
		
	AL_Remove(stack->list, AL_Begin(stack->list));
	
	stack->count--;
}

void *AS_Top(ArrayStack stack) {
	if (stack == NULL || stack->count == 0)
		return NULL;
		
	return AL_Get(stack->list, AL_Begin(stack->list));
}

